package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.model.JSONResult;
import com.example.demo.model.json.ConfigData;
import com.example.demo.respository.ConfigRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/config_action")
public class ConfigController {


    @Autowired
    private ConfigRepository configRepository;



    
    /**
     * 保存config
     */
    @PostMapping  (value = "save_config")
    public ModelAndView saveConfig(ConfigData app) {
        System.err.println("save_config====>>" + app.getName() + "    " + app.getId());
        app = configRepository.save(app);

        JSONResult jsonResult = new JSONResult();
        jsonResult.putJSONData(200, "save config success", "保存成功");

        return new ModelAndView("redirect:/config_action/config_detail?id="+app.getId());//这是action事件，不是html路径
//        return jsonResult;
    }
    
    /**
     * 修改config界面
     */
    @GetMapping(value = "edit/{id}")
    public ModelAndView editForm(@PathVariable("id") Long id, Model model) {
        System.err.println("config_edit====>>" + id);
        Optional<ConfigData> config = configRepository.findById(id);
        model.addAttribute("config", config.get());
        model.addAttribute("title", "编辑config");
        return new ModelAndView("blog_zone/config_edit", "configModel", model);
    }
    

    /**
     *直接通过接口 更新配置 
     * http://localhost:8090/config_action/config_update
     */
    @GetMapping("/config_update")
    public String config_update(HttpServletRequest request) {
        String id = request.getParameter("id");
        String content = request.getParameter("content");
        System.err.println("save  content====>>" + content);
        Optional<ConfigData> bbc = configRepository.findById(Long.parseLong(id));
        ConfigData configData = bbc.get();
        configData.setContent(content);
        configRepository.save(configData);

        JSONResult jsonResult = new JSONResult();
        jsonResult.putJSONData(200, "save  success"+configData.getName(),
        		configData.getContent());
        System.err.println("save  success====>>" + id);
        
        return configData.getContent();
    }


    /**
     * 视频详情  success
     * http://localhost:8090/config_action/config_detail
     */
    @GetMapping("/config_detail")
    public String videoDetail(HttpServletRequest request) {
        String id = request.getParameter("id");
        Optional<ConfigData> bbc = configRepository.findById(Long.parseLong(id));
        ConfigData videoData = bbc.get();

        JSONResult jsonResult = new JSONResult();
        jsonResult.putJSONData(200, "query  success"+videoData.getName(),
        		videoData.getContent());

        return videoData.getContent();
    }


    
    
    
}